#include "los_interrupt.h"
#include "los_compiler.h"
#include <stdio.h>
#include "los_task.h"
#include "osTest.h"

#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"


/* 中断处理程序 */
STATIC VOID UsrIrqEntry(VOID)
{
    printf("in the func UsrIrqEntry\n");
}

/* 注册的线程回调函数，用于触发中断 */
STATIC VOID InterruptTest(VOID)
{
    LOS_HwiTrigger(HWI_NUM_TEST);
}

UINT32 ExampleInterrupt(VOID)
{
    UINT32 ret;
    HWI_PRIOR_T hwiPrio = 3; // 3，中断优先级
    HWI_MODE_T mode = 0;
    HWI_ARG_T arg = 0;

    /* 创建中断 */
    ret = LOS_HwiCreate(HWI_NUM_TEST, hwiPrio, mode, (HWI_PROC_FUNC)UsrIrqEntry, arg);
    if(ret == LOS_OK){
        printf("-------------------\n");
        printf("Hwi create success!\n");
    } else {
        printf("Hwi create failed!\n");
        return LOS_NOK;
    }

    osThreadAttr_t attr;

    attr.name = "led_test";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 20*1024;
    attr.priority = osPriorityNormal;

    if (osThreadNew((osThreadFunc_t)InterruptTest, NULL, &attr) == NULL) {
        printf("[testTask] Falied to create Task!\n");
    }

    /* 延时50 tick，让出当前线程的调度 */
    LOS_TaskDelay(50);

    /* 删除注册的中断 */
    ret = LOS_HwiDelete(HWI_NUM_TEST, NULL);
    if(ret == LOS_OK){
        printf("Hwi delete success!\n");
    } else {
        printf("Hwi delete failed!\n");
        return LOS_NOK;
    }
    
    return LOS_OK;
}

APP_FEATURE_INIT(ExampleInterrupt);